﻿using System.Collections.Generic;
using System.Windows.Forms;

namespace NExplorer
{
	public abstract class AddIn : IAddIn
	{
		private readonly string					_name;
		private readonly string					_description;
		private readonly List<IMenuBuilder>		_menuBuilders = new List<IMenuBuilder>();
		private readonly List<IToolBarButton>	_toolBarButtons = new List<IToolBarButton>();

		public AddIn(string name, string description)
		{
			_name = name;
			_description = description;
		}

		public string Name
		{
			get { return _name; }
		}

		public string Description
		{
			get { return _description; }
		}

		protected void AddMenuBuilder(IMenuBuilder menuBuilder)
		{
			_menuBuilders.Add(menuBuilder);
			ServiceManager.MenuBuilderManager.AddMenuBuilder(menuBuilder);
		}

		protected void AddToolBarButton(ICommand command)
		{
			AddToolBarButton(command, Keys.None);
		}

		protected void AddToolBarButton(ICommand command, Keys shortcut)
		{
			IToolBarButton button = ServiceManager.ToolBar.AddButton(command, shortcut);
			_toolBarButtons.Add(button);
		}

		public abstract void Initialize();

		public virtual void Unload()
		{
			_menuBuilders.ForEach(handler => ServiceManager.MenuBuilderManager.RemoveMenuBuilder(handler));
			_toolBarButtons.ForEach(button => ServiceManager.ToolBar.RemoveButton(button));
		}
	}
}
